/*	This file is part of AstroAttack.
*
*	AstroAttack is free software; you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation; either version 2 of the License, or
*	(at your option) any later version.
*	
*	AstroAttack is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*	
*	You should have received a copy of the GNU General Public License
*	along with AstroAttack; if not, write to the Free Software
*	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*	
*	$Id: INIParser.java 76 2007-03-07 03:40:51Z cornfused36 $
*/

package AstroAttack;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.IOException;

import java.util.Map;
import java.util.TreeMap;
import java.util.Scanner;

public class INIParser
{
	private File m_file;
	private Map<String, Map<String, String>> m_entries;
	
	public INIParser(String iniFile)
	{
		this(new File(iniFile));
	}
	
	public INIParser(File iniFile)
	{
		m_file = iniFile;
		m_entries = new TreeMap<String, Map<String, String>>();
		System.out.println(m_file.getParent());
	}
	
	public File getFile()
	{
		return m_file;
	}
	
	public boolean load()
	{
		Debug.println("Parsing INI file: " + m_file);
		Scanner s;
		try
		{
			s = new Scanner(m_file);
		}
		catch ( FileNotFoundException e )
		{
			Debug.println("\tfile not found");
			return false;
		}
		
		String section = "";
		int lineNumber = 0;
		
		while( s.hasNextLine() )
		{
			String line = s.nextLine().trim();
			++lineNumber;
			
			if ( line.length()==0 )
				continue;
			
			if ( line.startsWith("[") && line.endsWith("]") )
			{
				section = line.substring(1, line.length() - 1);
				m_entries.put(section, new TreeMap<String, String>());
			}
			else if ( line.indexOf('=') > 0 )
			{
				String key = line.substring(0, line.indexOf('=')).trim();
				String val = line.substring(line.indexOf('=') + 1).trim();
				
				if ( !m_entries.containsKey(section) )
					m_entries.put(section, new TreeMap<String, String>());
				
				m_entries.get(section).put(key, val);
			}
			// ignore comments
			else if ( line.startsWith(";") || line.startsWith("#") );
			else
			{
				Debug.println("\tsyntax error: " + m_file + ":" + lineNumber);
				return false;
			}
		}
		
		s.close();
		
		return true;
	}
	
	public String get(String section, String key)
	{
		return m_entries.get(section).get(key);
	}
	
	public void set(String section, String key, String value)
	{
		m_entries.get(section).put(key, value);
	}
	
	public void save()
	{
		try
		{
			Debug.print("Saving INI file " + m_file.getPath() + "...  ");
			PrintWriter bw = new PrintWriter(m_file);
			
			for ( Map.Entry<String, Map<String, String>> e : m_entries.entrySet() )
			{
				String line = "[" + e.getKey() + "]";
				bw.println(line);
				bw.flush();
				
				for ( Map.Entry<String, String> f : e.getValue().entrySet() )
				{
					String line2 = f.getKey() + "=" + f.getValue();
					bw.println(line2);
					bw.flush();
				}
			}
			
			Debug.println("done");
		}
		catch ( IOException e )
		{
			Debug.println("failed:");
			Debug.println(e);
		}
		
		// then reload, just so the entries map is updated
		load();
	}
}
